<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Core_Collection implements Iterator, Countable {

	protected $_items = null;

	protected $_position = 0;
	
	protected $_itemClass = 'Core_Entity';
	
	/**
	 * Prepare items of collection (implemented by child class)
	 */
	protected function _prepareData() {
		$this->_items = array();
	}
	
	/**
	 * Set item object class type
	 */
	public function setItemClass($class) {
		$this->_itemClass = $class;
	}

	/**
	 * Reset collection
	 */
	public function reset() {
		$this->_items = null;
		$this->_position = 0;
	}

	public function current() {
		$this->_prepareData();
		$obj = App::object($this->_itemClass);
		$obj->addData($this->_items[$this->_position]);
		return $obj;
	}
	
	public function key() {
		return $this->_position;
	}
	
	public function next() {
		++$this->_position;
	}
	
	public function rewind() {
		$this->_position = 0;
	}
	
	public function valid() {
		$this->_prepareData();
		return isset($this->_items[$this->_position]);
	}
	
	public function count() {
		$this->_prepareData();
		return count($this->_items);
	}
	
	public function toArray() {
		$this->_prepareData();
		return $this->_items;
	}
	
	/*
	public function load() {
		$this->_prepareData();
		return $this;
	}
	*/
}